package cn.stylefeng.guns.goods.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.core.annotion.BusinessLog;
import cn.stylefeng.guns.core.enums.LogAnnotionOpTypeEnum;
import cn.stylefeng.guns.core.pojo.response.ErrorResponseData;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.core.pojo.response.SuccessResponseData;
import cn.stylefeng.guns.goods.entity.GoodData;
import cn.stylefeng.guns.goods.dto.GoodsDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.stylefeng.guns.goods.service.GoodsService;
import cn.stylefeng.guns.goods.model.param.GoodsParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * goods 控制器
 *
 * @author: aria
 * @date: 2021/02/20 20:24
 */
@Api(tags = "商品相关")
@Slf4j
@RestController
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    /**
     * 分页查询goods
     *
     * @author aria
     * @date 2021/02/20 20:24
     */
    @ApiOperation("分页查询goods")
    @GetMapping("/goods/page")
    @BusinessLog(title = "goods_分页查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData page(@RequestParam(required = false,defaultValue = "0") Integer audit, GoodsParam goodsParam) {
        goodsParam.setAudit(audit);
        GoodsDTO goodsDTO=new GoodsDTO();
        BeanUtil.copyProperties(goodsParam,goodsDTO);
        return new SuccessResponseData(goodsService.page(goodsDTO));
    }

    /**
     * 获取全部goods
     *
     * @author aria
     * @date 2021/02/20 20:24
     */
    @ApiOperation("获取全部goods")
    @GetMapping("/goods/list")
    @BusinessLog(title = "goods_查询所有", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData list(GoodsParam goodsParam) {
        GoodsDTO goodsDTO=new GoodsDTO();
        BeanUtil.copyProperties(goodsParam,goodsDTO);
        return new SuccessResponseData(goodsService.list(goodsDTO));
    }
    /**
     * 获取某一类的所有商品
     *
     * @author aria
     * @date 2021/02/20 20:24
     */
    @ApiOperation("获取某一类的所有商品,只需要传type")
    @GetMapping("/goods/listByType")
    @BusinessLog(title = "获取某一类的所有商品,只需要传type", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData listByType(GoodsParam goodsParam) {
        if (goodsParam.getType() == null){
            return new ErrorResponseData(500,"商品分类不能为空");
        }
        GoodsDTO goodsDTO=new GoodsDTO();
        BeanUtil.copyProperties(goodsParam,goodsDTO);
        return new SuccessResponseData(goodsService.listByType(goodsDTO));
    }
    @ApiOperation("获取某用户上传的所有商品,只需要传userId")
    @GetMapping("/goods/listByUserId")
    @BusinessLog(title = "获取某用户上传的所有商品,只需要传userId", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData listByUserId(GoodsParam goodsParam) {
        if (goodsParam.getUserId() == null){
            return new ErrorResponseData(500,"用户编号不能为空");
        }
        GoodsDTO goodsDTO=new GoodsDTO();
        BeanUtil.copyProperties(goodsParam,goodsDTO);
        return new SuccessResponseData(goodsService.listByUserId(goodsDTO));
    }
    /**
     * 获取精选商品
     *
     * @author aria
     * @date 2021/02/20 20:24
     */
    @ApiOperation("获取精选商品")
    @GetMapping("/goods/popList")
    @BusinessLog(title = "获取精选商品", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData popList(GoodsParam goodsParam) {
        GoodsDTO goodsDTO=new GoodsDTO();
        BeanUtil.copyProperties(goodsParam,goodsDTO);
        return new SuccessResponseData(goodsService.popList(goodsDTO));
    }
    /**
     * 获取热销商品
     *
     * @author aria
     * @date 2021/02/20 20:24
     */
    @ApiOperation("获取热销商品")
    @GetMapping("/goods/sellList")
    @BusinessLog(title = "获取热销商品", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData sellList(GoodsParam goodsParam) {
        GoodsDTO goodsDTO=new GoodsDTO();
        BeanUtil.copyProperties(goodsParam,goodsDTO);
        return new SuccessResponseData(goodsService.sellList(goodsDTO));
    }
    /**
     * 获取最新商品
     *
     * @author aria
     * @date 2021/02/20 20:24
     */
    @ApiOperation("获取最新商品")
    @GetMapping("/goods/newList")
    @BusinessLog(title = "获取精选商品", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData newList(GoodsParam goodsParam) {
        GoodsDTO goodsDTO=new GoodsDTO();
        BeanUtil.copyProperties(goodsParam,goodsDTO);
        return new SuccessResponseData(goodsService.newList(goodsDTO));
    }
    /**
     * 根据商品名模糊查询
     *
     * @author aria
     * @date 2021/02/20 20:24
     */
    @ApiOperation("根据商品名模糊查询")
    @GetMapping("/goods/selectLikeName")
    @BusinessLog(title = "根据商品名模糊查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData selectLikeName(GoodsParam goodsParam) {
        GoodsDTO goodsDTO=new GoodsDTO();
        BeanUtil.copyProperties(goodsParam,goodsDTO);
        return new SuccessResponseData(goodsService.selectLikeName(goodsDTO));
    }


    /**
     * 查看详情goods
     *
     * @author aria
     * @date 2021/02/20 20:24
     */
    @ApiOperation("根据商品id获取商品详情(只需要传id)")
    @GetMapping("/goods/detail")
    @BusinessLog(title = "goods_查看详情", opType = LogAnnotionOpTypeEnum.DETAIL)
    public ResponseData detail(GoodsParam goodsParam) {
        GoodsDTO goodsDTO=new GoodsDTO();
        BeanUtil.copyProperties(goodsParam,goodsDTO);
        return new SuccessResponseData(goodsService.detail(goodsDTO));
    }

    /**
     * 添加goods
     *
     * @author aria
     * @date 2021/02/20 20:24
     */
    @ApiOperation("新增商品")
    @PostMapping("/goods/add")
    @BusinessLog(title = "goods_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData add(@Validated(GoodsParam.add.class)@RequestBody GoodsParam goodsParam) {
        GoodsDTO goodsDTO=new GoodsDTO();
        BeanUtil.copyProperties(goodsParam,goodsDTO);
        return goodsService.add(goodsDTO);
    }

    /**
     * 删除goods
     *
     * @author aria
     * @date 2021/02/20 20:24
     */
    @ApiOperation("根据商品id删除商品(只需要传id)")
    @PostMapping("/goods/delete")
    @BusinessLog(title = "goods_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public ResponseData delete(@RequestBody GoodsParam goodsParam) {

        GoodsDTO goodsDTO=new GoodsDTO();
        BeanUtil.copyProperties(goodsParam,goodsDTO);
        goodsService.delete(goodsDTO);
        return new SuccessResponseData();
    }

    /**
     * 编辑goods
     *
     * @author aria
     * @date 2021/02/20 20:24
     */


    @PostMapping("/goods/edit")
    @BusinessLog(title = "goods_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData edit(@RequestBody GoodsParam goodsParam) {
        GoodsDTO goodsDTO=new GoodsDTO();
        BeanUtil.copyProperties(goodsParam,goodsDTO);
        goodsService.edit(goodsDTO);
        return new SuccessResponseData();
    }

    @ApiOperation("饼状图数据")
    @PostMapping("/goods/data")
    @BusinessLog(title = "饼状图数据")
    public ResponseData data() {
        List<GoodData> data =  goodsService.data();
        return new SuccessResponseData(data);
    }


}
